package com.ds.esd.bpm.custom.perform;

import com.ds.bpm.client.ActivityInst;
import com.ds.bpm.client.ProcessDefForm;
import com.ds.bpm.client.ProcessDefVersion;
import com.ds.bpm.client.RouteToType;
import com.ds.bpm.client.data.DataMap;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.WorkflowClientService;
import com.ds.common.JDSException;
import com.ds.common.database.dao.DBMap;
import com.ds.config.ListResultModel;
import com.ds.enums.db.MethodChinaName;

import com.ds.esd.client.ESDFacrory;
import com.ds.esd.client.ProjectVersion;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.api.enums.ResponsePathEnum;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTabsViewAnnotation;
import com.ds.esd.custom.toolbar.bpm.RouteCustomMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.repository.database.proxy.DSMTableProxy;
import com.ds.esd.tool.module.EUModule;
import com.ds.esd.tool.ui.component.Component;
import com.ds.esd.util.TabPageUtil;
import com.ds.jds.core.esb.EsbUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/bpm/custom/")
@MethodChinaName(cname = "流程流转", imageClass = "bpmfont bpmqidonggongzuoliu")
public class PerformService {
    @DynLoadAnnotation()
    @RequestMapping(method = RequestMethod.POST, value = "FormData")
    @ModuleAnnotation(caption = "装载表单", cache = false)
    @APIEventAnnotation(bindMenu = CustomMenuItem.tabEditor)
    @ResponseBody
    public ListResultModel<List<Component>> getForm(String activityInstId, String processInstId, String tableName) {
        ListResultModel<List<Component>> formModule = new ListResultModel<List<Component>>();
        try {
            if (activityInstId != null) {
                ActivityInst activityInst = this.getClient().getActivityInst(activityInstId);
                ProcessDefVersion processDefVersion = activityInst.getProcessDefVersion();
                ProjectVersion version = ESDFacrory.getESDClient().getProjectVersionByName(processDefVersion.getClassification());
                String viewInstId = processDefVersion.getAttribute("viewInstId");
                DSMTableProxy proxy = DSMFactory.getInstance().getRepositoryManager().getTableProxyByName(tableName, viewInstId);
                DataMap map = activityInst.getFormValues();
                DBMap dbMap = (DBMap) map.get(proxy.getFieldName());
                EUModule tableModule = ESDFacrory.getESDClient().createTableModule(version.getVersionName(), tableName, dbMap, viewInstId);
                tableModule.getComponent().getTopComponentBox().setAlias(tableName);
                formModule.setData(tableModule.getComponent().getTopComponents(true));
            }

        } catch (JDSException e) {
            e.printStackTrace();
        }
        return formModule;
    }


    @RequestMapping(method = RequestMethod.POST, value = "Display")
    @RouteCustomMenu(routeType = {RouteToType.Reload})
    @NavTabsViewAnnotation
    @DynLoadAnnotation()
    @ModuleAnnotation(caption = "办理", dynLoad = true, name = "table")
    @APIEventAnnotation(bindMenu = CustomMenuItem.editor, customRequestData = RequestPathEnum.ctx, customResponseData = ResponsePathEnum.form)
    @ResponseBody
    public ListResultModel<List<FormTabs>> display(String activityInstId, String processInstId) {
        ListResultModel<List<FormTabs>> formTabsTree = new ListResultModel<>();
        List<DSMTableProxy> tableProxyList = new ArrayList<>();
        try {
            if (activityInstId != null) {
                ActivityInst activityInst = this.getClient().getActivityInst(activityInstId);
                ProcessDefVersion processDefVersion = activityInst.getProcessDefVersion();
                String domainId = processDefVersion.getAttribute("domainId");
                ProcessDefForm formDef = processDefVersion.getFormDef();
                List<String> tableNames = formDef.getTableNames();
                for (String tableName : tableNames) {
                    DSMTableProxy proxy = DSMFactory.getInstance().getRepositoryManager().getTableProxyByName(tableName, domainId);
                    tableProxyList.add(proxy);
                }
            }
            formTabsTree = TabPageUtil.getTabList(tableProxyList, FormTabs.class);
        } catch (BPMException e) {
            e.printStackTrace();
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return formTabsTree;
    }

    /**
     * @return
     */
    public WorkflowClientService getClient() {

        WorkflowClientService client = ((WorkflowClientService) EsbUtil.parExpression("$BPMC"));

        return client;
    }
}
